// pages/classify/classify.js
Page({
  data: {
    globalData: getApp().globalData,
    keyword: '',
    topCategory: [],
    category: [],
    current: 1,
    currentName: ''
  },

  onLoad(options) {
    this.getTopCategory()
  },

  select() {
    wx.navigateTo({
      url: '/pages/classifyList/classifyList?keyword=' + this.data.keyword,
    })
  },
  getTopCategory() {
    let that = this
    wx.request({
      url: that.data.globalData.server + '/category/query?categoryTopId=0',
      method: 'GET',
      success(res) {
        that.setData({
          topCategory: res.data.data.records,
          current: res.data.data.records[0].categoryId,
          currentName: res.data.data.records[0].categoryName
        })
      },
      complete() {
        that.getCagetory(that.data.current)
      }
    })
  },
  getCagetory(id) {
    let that = this
    wx.request({
      url: that.data.globalData.server + '/category/query?categoryTopId=' + id,
      method: 'GET',
      success(res) {
        that.setData({
          category: res.data.data.records
        })
      }
    })
  },
  switchTop(e) {
    let top = e.currentTarget.dataset.top
    this.setData({
      current: top.categoryId, 
      currentName: top.categoryName
    })
    this.getCagetory(top.categoryId)
  },
  toList(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/classifyList/classifyList?id=' + id,
    })
  }
  
})